package com.ajun.common.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import java.io.IOException;

/**
 * ES Client Factory
 *
 * @author AJun
 * @date 2021-07-20
 */
@Slf4j
public class EsClientFactory {

    private static final RestHighLevelClient INSTANCE;
    public static String DEFAULT_URL = "http://localhost:9200";

    static {
        INSTANCE = new RestHighLevelClient(RestClient.builder(HttpHost.create(DEFAULT_URL)));
    }

    public static RestHighLevelClient esClient() {
        return INSTANCE;
    }

    public static void close() {
        try {
            INSTANCE.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            log.debug("Elasticsearch connection is closed.");
        }
    }
}
